<script lang="ts" setup name="AdjustmentDialog">
import { FormInstance, FormRules } from 'element-plus'
import { Dialog } from '~/src/api/interface'
import { Procure } from '~/src/api/interface/procure'
import { adjustmentType, stockFlagType } from '~/src/utils/serviceDict'
const formRef = ref<FormInstance>()
const dialogParams = ref<Dialog<Partial<Procure.AdjustmentReason>>>({
  title: '',
  visible: false,
  data: {},
})

const rules = reactive<FormRules>({
  content: [{ required: true, message: '请输入' }],
  name: [{ required: true, message: '请输入' }],
  reasonType: [{ required: true, message: '请选择' }],
  stockFlag: [{ required: true, message: '请选择' }],
})

const putParams = async (data: Dialog<Partial<Procure.AdjustmentReason>>) => {
  dialogParams.value.data = data.data
  dialogParams.value = data
  dialogParams.value.visible = true
}
const onConfirm = async () => {
  await formRef.value!.validate()
  await useHandleDialog(
    dialogParams.value.api as () => Promise<any>,
    dialogParams.value.data,
    dialogParams.value.getTableList as () => void
  )
  dialogParams.value.visible = false
}
watch(
  () => dialogParams.value,
  (val) => {
    if (!val) formRef.value?.resetFields()
  }
)
defineExpose({ putParams })
</script>

<template>
  <vab-dialog v-model:modelValue="dialogParams.visible" :title="dialogParams.title" width="500">
    <el-form ref="formRef" label-suffix="：" label-width="110px" :model="dialogParams.data" :rules="rules">
      <el-form-item label="原因名称" prop="content">
        <el-input v-model="dialogParams.data.content" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="原因类型" prop="reasonType">
        <el-select v-model="dialogParams.data.reasonType" placeholder="">
          <el-option v-for="item in adjustmentType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="出入标记" prop="stockFlag">
        <el-select v-model="dialogParams.data.stockFlag" placeholder="">
          <el-option v-for="item in stockFlagType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dialogParams.data.remark" placeholder="" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="onConfirm">保存</el-button>
      <el-button @click="dialogParams.visible = false">取消</el-button>
    </template>
  </vab-dialog>
</template>

<style lang="scss" scoped></style>
